package mdm.led.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.common.LocalConfig;
import mdm.led.entity.five.FiveDetails;
import mdm.led.entity.five.FiveItme;
import mdm.led.entity.five.FiveProcedure;
import mdm.led.service.FiveProcedureService;
import mdm.led.util.ListToString;

/**
 * @author :Monarch丿He
 * @date :2018年11月4日
 * @explain :流程管控 App
 */
@Controller
@RequestMapping("fiveProcedureApp")
@Api(description = "流程管控 App")
public class FiveProcedureAppController {

	@Autowired
	protected LocalConfig localConfig;

	private static final Logger LOGGER = LoggerFactory.getLogger(FiveProcedureAppController.class);

	@Autowired
	private FiveProcedureService fiveProcedureServiceImpl;

	@ApiOperation("查询流程管控表格列表信息")
	@ResponseBody
	@RequestMapping(value = "findProcedureTablesInfo", method = RequestMethod.GET)
	public AjaxResponse findProcedureTablesInfo(@RequestParam(value = "Tablename", required = false) String name) {

		LOGGER.info("\n-------------- 查询路程管控共表格列表信息 ------------");
		List<FiveProcedure> list = fiveProcedureServiceImpl.findFiveProcedure(name);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(list) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);

	}

	@ApiOperation("查询流程管控某表下所有详情表 ")
	@ResponseBody
	@RequestMapping(value = "findFiveDetailsList", method = RequestMethod.GET)
	public AjaxResponse findFiveDetailsList(@RequestParam(value = "username", required = false) String username,
			@RequestParam("procedureId") Integer procedureId) {

		LOGGER.info("\n-------------- 查询流程管控某表下所有详情表 ------------");
		List<FiveDetails> list = fiveProcedureServiceImpl.findFiveDetailsList(username, procedureId);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(list) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
	}

	@ApiOperation("查询流程管控某表表结构及信息 ")
	@ResponseBody
	@RequestMapping(value = "findProcedureByPidAndUid", method = RequestMethod.GET)
	public AjaxResponse findFiveProcedure(@RequestParam("procedureId") Integer procedureId,
			@RequestParam("userId") Integer userId) {

		LOGGER.info("\n-------------- 查询流程管控某表表结构及信息 ------------");
		FiveProcedure fiveProcedure = fiveProcedureServiceImpl.findProcedureByPidAndUid(procedureId, userId);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(fiveProcedure) + " ------------");

		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, fiveProcedure);
	}

	@ApiOperation("修改流程管理详情信息 ")
	@ResponseBody
	@RequestMapping(value = "updateFiveDetails", method = RequestMethod.POST)
	public AjaxResponse updateFiveDetails(HttpServletRequest request) throws InterruptedException {
		LOGGER.info("\n---------修改流程管理详情信息----------");
		// 存放图片的路径
		String imgPath = localConfig.getPicture_upload();
		AjaxResponse ajaxResponse = null;
		// {---解析实体---}
		// JSON 字符串
		String body = request.getParameter("body");
		body = body.replaceAll("null","");
		// json 转对象
		FiveDetails details = JSON.parseObject(body, FiveDetails.class);
		// {---解析图片---}
		try {
			Collection<Part> parts = request.getParts();
			// 文件后缀
			String suffix = "";
			// 文件名
			String fileName = "";
			// 图片集合
			List<String> list = new ArrayList<>();
			// 防止重命名标识
			int i = 0;
			// 循环解析
			for (Part part : parts) {
				if (!part.getName().toString().equals("body")) {
					i ++;
					fileName = part.getName();
					suffix = fileName.substring(fileName.lastIndexOf("."));
					// 写入文件到 指定文件夹 imgPath
					String file = String.valueOf(System.currentTimeMillis()) + i + suffix;
					// 添加到 文件集合
					list.add(file);
					// 写入文件
					part.write(imgPath + file);
				}
			}
			if(list.size() > 0){
				String imgStr = ListToString.stringListToString(list, ",");
				// 循环分类放入实体
				for (FiveItme item : details.getItems()) {
					// 类型为图片
					if(item.getType() == 2){
						item.setValue(imgStr);
					}
				}
			}
			
			boolean iRet = fiveProcedureServiceImpl.updateFiveDetails(details);
			if(iRet) {
				ajaxResponse = AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "OK");
			}else{
				ajaxResponse = AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "NO");
			}
			LOGGER.info("\n-------------- 结果:" + iRet + " ------------");
		} catch (IOException | ServletException e) {
			e.printStackTrace();
		}
		
		return ajaxResponse;
		
	}

}
